﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Reporting.WebForms;
using System.Data;
using Runda.CMS.Bll;
using Runda.CMS.Common.Entity;
using System.Text;

namespace Runda.CMS.Portal.Report
{
    public partial class SalesRpt_I2 : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                for (int i = 2012; i <= 2015; i++)
                {
                    ddlBudgetYear.Items.Add(new ListItem(i.ToString(), i.ToString()));
                }
                

                string sqlWhere = getWhereClause();
                LoadContractTrack(sqlWhere);

            }
        }

        private void setdrData(string strHead1,string strHead2, ref DataTable dt,string sqlOldWhere, string strsqlWhere,BillBll bll)
        {
            DataRow dr;
            DataRow drtmp;
            DataTable dttmp = bll.GetSaleRevenue(sqlOldWhere + strsqlWhere);

            dr = dt.NewRow();
            drtmp = dttmp.Rows[0];

            dr["col1"] = strHead1;
            dr["col2"] = strHead2;
            dr["col3"] = drtmp["Amount"];
            dr["col4"] = drtmp["Cost"];

            dt.Rows.Add(dr);

        }
        private void LoadContractTrack(string sqlWhere)
        {
            //set reference
            ReportViewer ReportViewer = rvReporting;

            //get rdlc path
            string strRootPath = this.Server.MapPath(this.Request.ApplicationPath);
            string strFilePath ;

            strFilePath = "report/rdlc/SalesRpt_I2.rdlc";

            //set report viewer property
            ReportViewer.SizeToReportContent = true;
            ReportViewer.ZoomMode = ZoomMode.Percent;
            ReportViewer.AsyncRendering = false;
            
            //set report viewer work mode
            ReportViewer.ProcessingMode = ProcessingMode.Local;
            
            LocalReport lp = ReportViewer.LocalReport;
            lp.ReportPath = strFilePath;
            
            //get dataset  
            BillBll bll = new BillBll();
            //DataSet ds = new DataSet();
            //ds = bll.SalesRpt_E1(sqlWhere, ddlBudgetYear.SelectedValue + "-" + ddlBudgetMonth.SelectedValue, ddlBudgetYear.SelectedValue + "-" + ddlBudgetMonth1.SelectedValue);

            //ds.DataSetName = "SalesDataSet_rpt_I1";
            //set data set
            ReportDataSource rds = new ReportDataSource();
            rds.Name = "SalesDataSet_rpt_I1";

            DataTable dt = new DataTable();
            dt.Columns.Add(new DataColumn("col1", typeof(string)));
            dt.Columns.Add(new DataColumn("col2", typeof(string)));
            dt.Columns.Add(new DataColumn("col3", typeof(decimal)));
            dt.Columns.Add(new DataColumn("col4", typeof(decimal)));

            setdrData("强生", "其中：仪器", ref dt, sqlWhere, " and CategoryCode like '0201%'", bll);
            setdrData("", "试剂和耗材", ref dt, sqlWhere, " and CategoryCode like '0101%'", bll);

            setdrData("雅培", "其中：仪器", ref dt, sqlWhere, " and CategoryCode like '0202%'", bll);
            setdrData("", "试剂和耗材", ref dt, sqlWhere, " and CategoryCode like '0102%'", bll);

            setdrData("SYSMEX", "其中：仪器", ref dt, sqlWhere, " and CategoryCode like '0206%'", bll);
            setdrData("", "试剂和耗材", ref dt, sqlWhere, " and CategoryCode like '0112%'", bll);

            setdrData("西门子", "其中：仪器", ref dt, sqlWhere, " and CategoryCode like '0212%'", bll);
            setdrData("", "试剂和耗材", ref dt, sqlWhere, " and CategoryCode like '0116%'", bll);

            setdrData("积水", "其中：仪器", ref dt, sqlWhere, " and CategoryCode like '2222%'", bll);
            setdrData("", "试剂和耗材", ref dt, sqlWhere, " and CategoryCode like '0103%'", bll);
            setdrData("思塔高", "其中：仪器", ref dt, sqlWhere, " and CategoryCode like '0205%'", bll);
            setdrData("", "试剂和耗材", ref dt, sqlWhere, " and CategoryCode like '0105%'", bll);
            setdrData("伯乐", "其中：仪器", ref dt, sqlWhere, " and CategoryCode like '0203%'", bll);
            setdrData("", "试剂和耗材", ref dt, sqlWhere, " and CategoryCode like '0104%'", bll);
            setdrData("科宝", "其中：仪器", ref dt, sqlWhere, " and CategoryCode like '0211%'", bll);
            setdrData("", "试剂和耗材", ref dt, sqlWhere, " and CategoryCode like '0115%'", bll);
            setdrData("Drew", "其中：仪器", ref dt, sqlWhere, " and CategoryCode like '0208%'", bll);
            setdrData("", "试剂和耗材", ref dt, sqlWhere, " and CategoryCode like '0111%'", bll);
            setdrData("Thermofisher", "其中：仪器", ref dt, sqlWhere, " and CategoryCode like '0209%'", bll);
            setdrData("", "试剂和耗材", ref dt, sqlWhere, " and CategoryCode like '1111%'", bll);
            setdrData("Primus", "其中：仪器", ref dt, sqlWhere, " and CategoryCode like '0207%'", bll);
            setdrData("", "试剂和耗材", ref dt, sqlWhere, " and CategoryCode like '0110%'", bll);
            setdrData("ROCHE", "其中：仪器", ref dt, sqlWhere, " and CategoryCode like '0213%'", bll);
            setdrData("", "试剂和耗材", ref dt, sqlWhere, " and CategoryCode like '0118%'", bll);
            setdrData("BD", "其中：仪器", ref dt, sqlWhere, " and CategoryCode like '0204%'", bll);
            setdrData("", "试剂和耗材", ref dt, sqlWhere, " and CategoryCode like '0107%'", bll);
            setdrData("Binding Site", "其中：仪器", ref dt, sqlWhere, " and CategoryCode like '0217%'", bll);
            setdrData("", "试剂和耗材", ref dt, sqlWhere, " and CategoryCode like '0117%'", bll);
            setdrData("华臣", "其中：仪器", ref dt, sqlWhere, " and CategoryCode like '2222%'", bll);
            setdrData("", "试剂和耗材", ref dt, sqlWhere, " and CategoryCode like '0106%'", bll);
            setdrData("惠中", "其中：仪器", ref dt, sqlWhere, " and CategoryCode like '0210%'", bll);
            setdrData("", "试剂和耗材", ref dt, sqlWhere, " and CategoryCode like '0109%'", bll);
           
            setdrData("其他", "其中：仪器", ref dt, sqlWhere, " and left(CategoryCode,2)='02' and left(CategoryCode,4) not in ('0201','0202','0206','0212','0205','0203','0211','0208','0209','0207','0213','0204','0217','0210')", bll);
            setdrData("", "试剂和耗材", ref dt, sqlWhere, " and left(CategoryCode,2)='01' and CategoryCode like '0199%'", bll);

            setdrData("小计", "仪器", ref dt, sqlWhere, " and CategoryCode like '02%'", bll);
            setdrData("", "试剂和耗材", ref dt, sqlWhere, " and CategoryCode like '01%'", bll);
            setdrData("", "零配件", ref dt, sqlWhere, " and left(CategoryCode,2) in ('03','04','99')", bll);
            setdrData("", "维修保养", ref dt, sqlWhere, " and CategoryCode like '07%'", bll);

            setdrData("合计", "", ref dt, sqlWhere, "", bll);


            
            rds.Value = dt;
            lp.DataSources.Clear();
            lp.DataSources.Add(rds);

            ReportParameter rp_Customername = new ReportParameter("rp_TotalAmount", dt.Rows[dt.Rows.Count-1]["col3"].ToString());
            ReportViewer.LocalReport.SetParameters(new ReportParameter[] { rp_Customername });
            ReportViewer.LocalReport.Refresh();;
            
        }

        private string getWhereClause()
        {
            StringBuilder sb = new StringBuilder();

            sb.Append(" and billYear = " + ddlBudgetYear.SelectedValue.ToString() );
            sb.Append(" and billMonthex >= " + ddlBudgetMonth.SelectedValue.ToString());
            sb.Append(" and billMonthex <= " + ddlBudgetMonth1.SelectedValue.ToString());

            return sb.ToString();
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            string sqlWhere = getWhereClause();
            LoadContractTrack(sqlWhere);
        }

        
    }
}
